home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h> /* standard input output macros */
- #include <ctype.h> /* standard c macros */
-
- #define NEWLINE '\n' /* crlf macro */
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- FILE *in, *out, *fopen();
- int c; /* input character */
- int start = 1; /* starting column to save */
- int end = 80; /* last column to save */
- int col; /* current column */
-
- out = stdout; /* set default output file */
- if (argc == 1) {
- printf("\nusage: pluck infile.ext <-o output.ext> <-s col> <-e col>\n\n");
- printf(" -o output.exe -- default to console\n");
- printf(" -s col -- starting column default is 1\n");
- printf(" -e col -- ending column default is 256\n");
- exit(0);
- }
- else /* open input file */
- if ((in = fopen(*(++argv), "r")) == NULL) {
- printf("cannot read %s\n",*(argv));
- exit(1);
- }
- argc -= 2;
- argc >>= 1;
- /* parse command line for valid arguments */
- while (argc-- > 0 && (*++argv)[0] == '-') {
- c = tolower((*argv)[1]);
- switch (c) {
- case 'o': /* output file specified */
- if ((out = fopen(*(++argv), "w")) == NULL) {
- printf("cannot open %s for output",*(argv));
- exit(1);
- }
- break;
- case 's': /* starting column specified */
- start = ctoi(*(++argv));
- printf("start = %d\n",start);
- if (start > end || start < 0) {
- printf("invalid starting column\n");
- exit(1);
- }
- break;
- case 'e': /* ending column specified */
- end = ctoi(*(++argv));
- printf("end = %d\n",end);
- if (end > 256 || end <= start) {
- printf("invalid ending column\n");
- exit(1);
- }
- break;
- default : /* someone no read instructions */
- printf("invalid option chosen %s\n",*argv);
- exit(1);
- break;
- }
- }
-
- col = 1;
- while ((c = getc(in)) != EOF) {
- if (c == NEWLINE) {
- col = 0;
- putc(NEWLINE, out);
- }
- else if (col >= start && col <= end)
- putc(c, out);
- col++;
- }
- }
-
- ctoi(in) /* character string to integer */
- char in[];
- {
- int accum = 0;
- int i;
-
- while(*in) {
- accum *= 10;
- accum += (*in++ - '0');
- }
- return(accum);
- }
-